$NG.AllReady(function (
	page,
	{
		useValuesChange,
		useClick,
		useUpdateRow,
		useUpdateRows,
		useDataIndexChange,
	}
) {
	console.log("Data ready event triggered");
	var mstform = $NG.getCmpApi("p_form_invoice_apply_m");
	var dgrid = $NG.getCmpApi("p_form_invoice_apply_d");

	//页面条件
	if (page.oType == "add" || page.oType == "edit") {
		$NG.execServer('getEpmInfoByUserID', {phid:$NG.getUser().userID},(res)=>{
			const datas = JSON.parse(res.data)[0].extendObjects;
            console.log(datas);
			console.log("拿去合同号带出来的发票类型", mstform.getItem("u_invoice_type"));
			//更新UI表格
			$NG.updateState((updater) => {
                //【注意这里的表名不能为控件ID，必须要是表名】
                updater.data.p_form_invoice_apply_m.setProps({
                    u_hr_apply: {
                        value: datas.hem_phid,
                        label: datas.hem_name,
                    },	
                });
            });
		
		})
		calculationPay();
	}
	{
	//表头字段变化逻辑BEGIN
	useValuesChange(({ args }) => {
		getCntMInfoByPhid();
	}, "p_form_invoice_apply_m.u_cnt");
	} //表头字段变化逻辑END


	//根据合同id获取合同信息
	function getCntMInfoByPhid(params) {
		$NG.execServer(
			"getCntMInfoByPhid",
			{ phid: mstform.getItem("u_cnt").getValue() },
			function (res) {
				const datas = JSON.parse(res.data)[0].extendObjects;
				//帮助字段赋值
				$NG.updateState((updater) => {
					console.log("获取和同数据", datas)
					updater.data.p_form_invoice_apply_m.setProps({
						u_cnt_amt: datas.cnt_sum_vat_fc,
						u_cnt_no:datas.pcm_no
					});
				});
			}
		);
	}
	// 通过项目过滤合同号 初始化
	cntMFilter()
    function cntMFilter() {
		console.log(`正在过滤`);
        $NG.updateUI((updater, state) => {
            updater.fieldSetForm.p_form_invoice_apply_m.u_cnt.setProps({
                onBeforeOpen: async () => {
                    var phid_pc = $NG.getCmpApi('p_form_invoice_apply_m').getItem('phid_pc').getValue();
					console.log(`项目名：`, phid_pc);
                    if (!phid_pc) {
                        $NG.alert("请先选择项目");
                        return false;
                    } else {
                        return true;
                    }
                },
                ...(!!$NG.getCmpApi('p_form_invoice_apply_m').getItem('phid_pc').getValue() && {
                    clientSqlFilter: {
                        bill_type: 1,//合同类型
                        phid_pc: $NG.getCmpApi('p_form_invoice_apply_m').getItem('phid_pc').getValue()
                    }
                }),
            });
        });
    }
	// 监听字段项目
	useValuesChange(({ args }) => {
		cntMFilter();
        getProjectInfo()
	}, "p_form_invoice_apply_m.phid_pc");


	getEpmInfoByUserID()
	//根据所选组织获取往来单位信息
    function getEpmInfoByUserID(params) {
        $NG.execServer('getEnterpriseInfoByPhid', { phid: mstform.getValues().phid_org }, function (res) {
			console.log(`获取组织信息`, JSON.stringify(res));
            const datas = JSON.parse(res.data)[0].extendObjects;
            console.log("组织信息", datas);
            $NG.updateState((updater) => {
                updater.data.p_form_invoice_apply_m.setProps({
                    u_xfmc: datas.compname,
                    u_xfsh: datas.unisocialcredit,
                    u_xfdz: datas.taxaddress,
                    u_xfdh: datas.taxtelephone,
                    u_xfyhzh: datas.bankno,
                    u_xfyh: datas.taxbankname,
                    u_xfhh: datas.taxaccountno,
                });
            });
        });
    }
	

    // 获取项目
    function getProjectInfo(isReturn = false, phid = mstform.getItem("phid_pc").getValue(), fun){
        if(!mstform.getItem("phid_pc").getValue()) return 
        try{
            setTimeout(()=>{
                $NG.execServer("getPtInfoByPhid",{phid: phid}, (res)=>{
                    const _data = JSON.parse(res.data)[0].extendObjects
                    console.log(`获取项目信息`, _data);
                    if(isReturn){
                        fun(_data)
                    } else {
                        $NG.updateState((updater)=>{
                            updater.data.p_form_invoice_apply_m.setProps({
                                u_pc_no: _data.pc_no, //项目编码
                                phid_org: {
                                    //事业部or组织
                                    value: _data.org_phid,
                                    label: _data.org_name,
                                },
                                phid_emp:{
                                    label:_data.project_manager_name,
                                    value:_data.project_manager_phid
                                },
                                u_sqgw: {
                                    //售前顾问
                                    value: _data.user_sqgw_phid,
                                    label: _data.user_sqgw_name,
                                },
                                u_regional_center: {
                                    //区域中心
                                    value: _data.regional_center_phid,
                                    label: _data.regional_center_name,
                                },
                                u_product_line: {
                                    //产品线
                                    value: _data.midwife_phid,
                                    label: _data.midwife_name,
                                },
                                u_dept: {
                                    //部门
                                    value: _data.dept_phid,
                                    label: _data.dept_name,
                                },
                                u_pc_type: {//项目类型
                                    value: _data.type_phid,
                                    label: _data.type_name,
                                },
                            })
                        })
                    }
                })
            },100)
        }catch(err){
            $NG.alert("获取项目信息发生失败请检查~")
        }
    }

	//新增部分合同详细
	//通过数量和单价计算金额
	function calculationPay() {
        const arr = [
            "p_form_invoice_apply_d.u_qty",
            "p_form_invoice_apply_d.u_vat_prc",
            "p_form_invoice_apply_d.v_amt",
        ];
		arr.forEach((item, index) => {
			useDataIndexChange(({ args, instance }) => {
				console.log(`监听----${item}`, args);
				args[0].u_amt = ((args[0].u_qty||0)*(args[0].u_vat_prc)||0)
                instance.updateRow(args[0])
			}, item);
		});
	}
});
